package Base.ProjectTest01.Service;

import Base.ProjectTest01.Domain.*;

import static Base.ProjectTest01.Service.Data.*;

/**
 * @author 枫城
 * @createTime 2022/10/18 21:50
 * @Description  负责将Data中的数据封装到Employee[]中，同时提供相关操作
 */
public class NameListService {

    private Employee[] employees;

    public NameListService(){
        //        根据项目提供的Data类构建相应大小的employees数组
        employees = new Employee[EMPLOYEES.length];

        for (int i = 0; i < employees.length; i++) {
            // 获取员工类型
            final int type = Integer.parseInt(EMPLOYEES[i][0]);
            // 获取该员工的基本信息
            final int id = Integer.parseInt(EMPLOYEES[i][1]);
            final String name = EMPLOYEES[i][2];
            final int age = Integer.parseInt(EMPLOYEES[i][3]);
            final double salary = Double.parseDouble(EMPLOYEES[i][4]);

            Equipment equipment; // 只声明，不处理
            Double bonus;
            int stock;

            switch (type){
                case Data.EMPLOYEE:
                    employees[i] = new Employee(id, name, age, salary);
                    break;
                case  Data.PROGRAMMER:
                    equipment = createEquipment(i);
                    employees[i] = new Programmer(id, name, age, salary, equipment);
                    break;
                case Data.DESIGNER:
                    equipment = createEquipment(i);
                    bonus = Double.parseDouble(EMPLOYEES[i][5]);
                    employees[i] = new Designer(id, name, age, salary, equipment, bonus);
                    break;
                case Data.ARCHITECT:
                    equipment = createEquipment(i);
                    bonus = Double.parseDouble(EMPLOYEES[i][5]);
                    stock = Integer.parseInt(EMPLOYEES[i][6]);
                    employees[i] = new Architect(id, name, age, salary, equipment,bonus, stock);
                    break;
            }
        }
    }

    /**
     * @author 枫城
     * @createTime 2022/10/18 22:17
     * @Description  获取指定的员工设备
     */
    private Equipment createEquipment(int i) {
        final int type = Integer.parseInt(EQUIPMENTS[i][0]);
        switch (type){
            case PC:
                return  new PC(EQUIPMENTS[i][1], EQUIPMENTS[i][2]);
            case NOTEBOOK:
                return new NoteBook(EQUIPMENTS[i][1], EQUIPMENTS[i][2]);
            case PRINTER:
                return new Printer(EQUIPMENTS[i][1], EQUIPMENTS[i][2]);
        }

        return null;
    }


    /**
     * @desc 给employees及其数组元素进行初始化
     * @return employees
     */
    public Employee[] getAllEmployees(){
        return employees;
    }

    /**
     * @author 枫城
     * @createTime 2022/10/19 16:08
     * @Description  返回指定员工对象
     */
    public Employee getEmployee(int id) throws TeamException {
        for (int i = 0; i < employees.length; i++) {
            if (employees[i].getId() == id){
                return employees[i];
            }
        }
        throw new TeamException("未找到指定员工！");
    }




}
